package com.perfma.xtest.tool;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileOutputStream;
import java.util.Map;

/**
 * @author: junhui.si
 * @date: 2020-10-23 14:52
 **/
@Slf4j
public class WordDemo2 {

    public static void main(String[] args) {
        try {
            String inputFileName = TestFileUtil.getPath() + "report-t.docx";

            XWPFTemplate template = XWPFTemplate.compile(inputFileName);

            // 配置，使用spring EL表达式
            XWPFTemplate.compile(inputFileName);

            Map<String, Object> map = Maps.newHashMap();

            StringBuilder sb = new StringBuilder();
            sb.append("{{title4_1}}").append(" \n\n");
            sb.append("{{title4_1_1}}").append(" \n\n");
            sb.append("{{#form4_1_1}}").append(" \n\n");
            sb.append("{{@image4_1_1}}").append(" \n\n");
            sb.append("{{title4_1_2}}").append(" \n\n");
            sb.append("{{#form4_1_2}}").append(" \n\n");
            sb.append("{{@image4_1_2}}").append(" \n\n");

            map.put("testData", sb.toString());


            template.render(map);

//            template.getXWPFDocument().createTOC();
            String outputFileName = TestFileUtil.getPath() + "report.docx";
            FileOutputStream out = new FileOutputStream(outputFileName);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (Exception e) {
            log.error("error.", e);
        }
    }
}
